#include "ODESolver.h"

ODESolver::ODESolver(void)
{
}

ODESolver::ODESolver( double timestep, LinearSystemSolver *ls, ParticleSystem *ps )
{
	SolverInitialize(timestep,ls,ps);
}

ODESolver::~ODESolver(void)
{
	Clear();
}

void ODESolver::Solve()
{
	ls->Solve();
	Update();
}

void ODESolver::Update()
{
	ls->GetAccleration(accleration);
	ForewardEuler();
}

void ODESolver::ForewardEuler()
{
	int index=0;

	for (int i=0;i<ps->particles.size();i++)
	{
		Particle *p=ps->particles[i];

		p->vel[0]+=accleration[index++]*dt;
		p->vel[1]+=accleration[index++]*dt;

		p->pos[0]+=p->vel[0]*dt;
		p->pos[1]+=p->vel[1]*dt;
	}

}

void ODESolver::SolverInitialize( double timestep, LinearSystemSolver *ls, ParticleSystem *ps )
{
	dt=timestep;
	this->ps=ps;
	this->ls=ls;
	accleration=(double*)malloc(ps->GetScale()*sizeof(double));
}

void ODESolver::Clear()
{
	delete [] accleration;
}
